<?php

namespace App\Listeners;

use App\Events\UserCommand;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use swoole_client;

class SendCommand
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserCommand  $event
     * @return void
     */
    public function handle(UserCommand $event)
    {
        $client = new swoole_client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', env('WEB_INNER_PORT')))
        {
            logger("SendCommand connect failed. Error: {$client->errCode}\n");
        }
        if($event->cmd != 'heart_beat_data')
        {
            $openId = session('openId');
            $result = $client->send("$openId $event->cmd $event->num 1");
        }else{
            $result = $client->send("$event->fd $event->cmd $event->num 2");
        }
        $client->close();

    }

}
